package io.renren.modules.basic.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.basic.entity.MtCcProductEntity;
import io.renren.modules.basic.entity.MtCcUnitInfoEntity;
import io.renren.modules.basic.service.MtCcProductService;
import io.renren.modules.basic.service.MtCcUnitInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 10:50:53
 */
@Api(description = "产品管理")
@RestController
@RequestMapping(value = "generator/mtccproduct")
public class MtCcProductController {
    @Autowired
    private MtCcProductService mtCcProductService;
    @Autowired
    private MtCcUnitInfoService mtCcUnitInfoService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表",notes="列表")
    @RequestMapping("/list")
    @RequiresPermissions("generator:mtccproduct:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mtCcProductService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 统计当前库存
     */
    @ApiOperation(value = "统计当前库存",notes="统计当前库存")
    @RequestMapping("/count")
    public R count(){
        List<MtCcProductEntity> mtCcProductEntityList = mtCcProductService.currentProductCount();

        return R.ok().put("list", mtCcProductEntityList);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "信息",notes="信息")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:mtccproduct:info")
    public R info(@PathVariable("id") Integer id){
		MtCcProductEntity mtCcProduct = mtCcProductService.getById(id);

        return R.ok().put("mtccproduct", mtCcProduct);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存",notes="保存")
    @RequestMapping("/save")
    @RequiresPermissions("generator:mtccproduct:save")
    public R save(@RequestBody MtCcProductEntity mtCcProduct){
        MtCcUnitInfoEntity unit = mtCcUnitInfoService.getOne(new QueryWrapper<MtCcUnitInfoEntity>().eq("unit_name", mtCcProduct.getUnitName()));
        mtCcProduct.setUnitId(unit.getUnitId());
        mtCcProduct.setCreateTime(new Date());
        mtCcProductService.save(mtCcProduct);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改",notes="修改")
    @RequestMapping("/update")
    @RequiresPermissions("generator:mtccproduct:update")
    public R update(@RequestBody MtCcProductEntity mtCcProduct){
        mtCcProduct.setUpdateTime(new Date());
		mtCcProductService.updateById(mtCcProduct);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除",notes="删除")
    @RequestMapping("/delete")
    @RequiresPermissions("generator:mtccproduct:delete")
    public R delete(@RequestBody Integer[] ids){
		mtCcProductService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 导出
     */
    @RequestMapping("/productList/for/excel")
    @ApiOperation(value = "导出excel" , notes = "导出excel")
    public void listForExcel(HttpServletResponse response, @RequestParam Map<String, Object> params)throws IOException {
        List<MtCcProductEntity> mtCcProductEntityList =
                mtCcProductService.list();
        //指定列表标题和工作表名称
        ExportParams param = new ExportParams("基础产品信息报表","sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(param,MtCcProductEntity.class,mtCcProductEntityList);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type","application/vnd.ms-excel");
//        response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+".xls");
        String fileName = "基础产品信息报表";
        response.setHeader("Content-Disposition","attachment;filename="+ new String(fileName.getBytes("utf-8"),"ISO-8859-1" ) +".xls");

        try {
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
